<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
<div class="fromPage">
    <form class="layui-form layui-form-pane" lay-filter="LAY-system-user-form">
        <div class="layui-form-item">
            <label for="" class="layui-form-label">头像</label>
            <div class="layui-input-block">
                <div class="layui-upload" id="avatar"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline"> <label class="layui-form-label must">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" lay-verify="required|username" lay-vertype="tips" placeholder="请输入用户名" class="layui-input">
                </div>
            </div>
            <div class="layui-inline"> <label class="layui-form-label must">显示名</label>
                <div class="layui-input-inline">
                    <input type="text" name="display_name" lay-verify="required" lay-vertype="tips" placeholder="请输入显示名" class="layui-input">
                </div>
            </div>
        </div>
        {{# if(d.params.mode === 'add') { }}
        <div class="layui-form-item">
            <div class="layui-inline"> <label class="layui-form-label must">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" placeholder="请输入密码" lay-verify="required|pass" lay-vertype="tips"  class="layui-input">
                </div>
            </div>
            <div class="layui-inline"> <label class="layui-form-label must">确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password_confirmation" lay-verify="required|pass_confirm" lay-vertype="tips" placeholder="请输入确认密码" class="layui-input">
                </div>
            </div>
        </div>
        {{# } }}
        <div class="layui-form-item">
            <label class="layui-form-label must">角色</label>
            <div class="layui-input-block">
                <div id="roles"></div>
            </div>
        </div>
        <div class="layui-form-item" pane="">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常">
                <input type="radio" name="status" value="0" title="禁用" checked>
            </div>
        </div>
    </form>
</div>
</script>
<script>
    layui.data.sendParams = function(params) {
        const mode = params.mode, fromData = params.data;
        layui.use(['index', 'xmSelect', 'fileUpload', 'baseMethods'], function () {
            const {form, xmSelect, fileUpload, baseMethods} = layui;
            const uploadObj = fileUpload.init({
                elem: '#avatar',
                name: 'avatar'
            }), roles = xmSelect.render({
                el: '#roles',
                name: 'roles',
                prop: { name: 'display_name', value: 'name' },
                layVerify: 'required',
                layVerType: 'tips',
                initValue: fromData.roles && typeof fromData.roles === 'object' ? fromData.roles : []
            });
            if (mode === 'add') {
                getRoles();
            }else if (mode === 'edit') {
                getRoles();
                uploadObj.update(fromData.avatar);
                form.val('LAY-system-user-form', fromData);
            }
            form.verify({
                username: function(value, item) {
                    if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) return '用户名不能有特殊字符';
                    if(/(^\_)|(\__)|(\_+$)/.test(value)) return '用户名首尾不能出现下划线\'_\'';
                    if(/^\d+\d+\d$/.test(value)) return '用户名不能全为数字';
                },
                pass: [/^[\S]{8,16}$/, '密码必须8到16位，且不能出现空格'],
                pass_confirm: function (value, item) {
                    let data = form.val("LAY-system-user-form");
                    if(!new RegExp(/^[\S]{8,16}$/).test(value)) return '密码必须8到16位，且不能出现空格';
                    if (data.password !== value) return '两次输入密码不一致';
                },
            });
            function getRoles() {
                baseMethods.request({
                    url: '/system/role/list',
                    done: function (result){
                        if (result.code === 200) {
                            roles.update({
                                data: result.data.rows,
                                autoRow: true,
                            });
                        }
                    }
                })
            }
        })
    }
</script>
